import { cloneDeep } from "lodash-es";
import { isEmpty } from "@/utils/validate";
import { Base64 } from "js-base64";

export function queryURLParams(url: string) {
  let pattern = /(\w+)=(\w+)/gi; //定义正则表达式
  let parames: Record<string, any> = {}; // 定义参数对象
  url.replace(pattern, (a: any, b: any, c: any) => {
    parames[b] = c;
  });
  return parames;
}

// 字符串转base64
export function encode(str: string) {
  return Base64.encode(str);
}
export function decode(base64: string) {
  return Base64.decode(base64);
}

/*
 $name 通用的递归组件
 $author xhcss
 @data 需要递归的数组
 @parentId 递归的父级id
 @parentName 递归的父级的key
 @id 本级的key
 @nonull 是否删除children=[] 空的对象，默认删除
*/
export function generalRecursion(
  data: any[],
  parentId: any = null,
  parentName: string = "parentId",
  id: string = "id",
  nonull: boolean = true,
  childrenName: string = "children"
): any[] {
  if (!Array.isArray(data) || data.length == 0) return [];
  data = cloneDeep(data);
  return data
    .filter((o) => o[parentName] == parentId)
    .map((v) => {
      v[childrenName] = generalRecursion(
        data,
        v[id],
        parentName,
        id,
        nonull,
        childrenName
      );
      nonull && isEmpty(v[childrenName]) ? delete v[childrenName] : v;
      return v;
    });
}

export function deleteNoChildren(arr: any[]) {
  arr.forEach((item, index) => {
    if (item.children && item.children.length > 0) {
      deleteNoChildren(item.children);
    } else {
      delete item.children;
    }
  });
  return arr;
}

interface FlattenOptions {
  ischildren: boolean;
}
/**
 *  扁平化
 * @param arr 数组
 * @param ischildren  是否保留节点的children
 * @returns
 */
export function flattenDeepTree(
  arr: any[],
  options: FlattenOptions = { ischildren: false }
) {
  if (!arr) return [];
  const { ischildren } = options;
  let list: any = [];
  arr.forEach((item: any) => {
    const { children, ...arg } = item;
    list.push(ischildren ? item : arg);
    list = list.concat(flattenDeepTree(children, { ischildren }));
  });
  return list;
}

/**
 * 防抖
 * @param {Function} func  回调函数
 * @param {number} time 间隔时间
 */
export const debounce = function (func: Function, time: number) {
  let timer: any;
  return function (this: any, ...args: any) {
    clearTimeout(timer);
    timer = setTimeout(() => {
      func.apply(this, args);
    }, time);
  };
};

/**
 * 节流
 * @param {Function} func  回调函数
 * @param {number} delay 间隔时间
 */
export const throttle = function (func: Function, delay: number = 1000) {
  let timer: any = null;
  return function (this: any, ...args: any) {
    if (timer) {
      return;
    }
    timer = setTimeout(() => {
      timer = null;
      func.apply(this, args);
    }, delay);
  };
};

export const pageChangeShowHide = function () {
  let hidden, state, visibilityChange;
  if (typeof document.hidden !== "undefined") {
    hidden = "hidden";
    state = "visibilityState";
    visibilityChange = "visibilitychange";
  } else if (typeof document.mozHidden !== "undefined") {
    hidden = "mozHidden";
    state = "mozvisibilityState";
    visibilityChange = "mozvisibilitychange";
  } else if (typeof document.msHidden !== "undefined") {
    hidden = "msHidden";
    state = "msvisibilityState";
    visibilityChange = "msvisibilitychange";
  } else if (typeof document.webkitHidden !== "undefined") {
    hidden = "webkitHidden";
    state = "webkitvisibilityState";
    visibilityChange = "webkitvisibilitychange";
  }
  return { hidden, state, visibilityChange };
};


export const copeCode = (str: string) => {
  const input = document.createElement("textarea");
  input.style.opacity = "0";
  input.value = str;
  document.body.appendChild(input);
  input.select();
  document.execCommand("Copy");
  document.body.removeChild(input);
  ElMessage({
    type: "success",
    message: "复制成功",
  });
};
